--[[
    玩家游戏实体
]]--

local blaze = require "blaze"
local game = require "club.threecard.sudoku"

local GameObject = require "entities.interfaces.GameObject"
local Client = require "entities.interfaces.Client"
local Player = require "entities.interfaces.Player"

local log = blaze.logging.get("SudokuPlayer")

local M = game.kbe.entity("SudokuPlayer", GameObject, Client, Player)

function M:__init__()
    GameObject.__init__(self)
    Client.__init__(self)
    Player.__init__(self)
end

--[[ =============================
             服务端接口
================================== ]]--

function M:solve(index, n)
    self:cellCall("solve", index, n)
end

--[[ =============================
             属性变更回调
================================== ]]--

local function update_progress(player)
    if player.seat.no == 0 then
        return
    end
    game.store.ingame.set_progress(player)
end

function M:set_missed(old)
    update_progress(self)
end

function M:set_max_miss(old)
    update_progress(self)
end

function M:set_total_puzzles(old)
    update_progress(self)
end

function M:set_solved_puzzles(old)
    update_progress(self)
end

function M:set_total_cells(old)
    update_progress(self)
end

function M:set_solved_cells(old)
    update_progress(self)
end

function M:set_begin_time(old)
    if self == game.kbe.player() then
        game.store.ingame.set_begin_time(self.begin_time)
    end
end

--[[ =============================
             服务端回调
================================== ]]--

function M:onEnterWorld()
end

function M:onLeaveWorld()
end

function M:onEnterSpace()
    game.store.ingame.player_enter(self)
end

function M:onLeaveSpace()
    game.store.ingame.player_leave(self)
end

function M:onDestroy()
    if self:isPlayer() then
        game.store.ingame.clear()
    end
end

function M:on_game_init(scene)
    log.debug("on_game_init " .. blaze.dump(scene))
    game.store.ingame.init_game(scene)
end

function M:on_init_board(cells)
    log.debug("on_init_board " .. blaze.dump(cells))
    game.store.ingame.init_board({
        cells = cells
    })
end

function M:on_solve_result(cell_index, number)
    log.debug(string.format("on_solve_result %s %s", cell_index, number))
    if number > 0 then
        game.store.ingame.set_answer_resolved({
            index = cell_index,
            value = number
        })
    else
        game.store.ingame.set_answer_rejected({
            index = cell_index
        })
    end
end

return M